class AdminController {

    def postService

    def index = {
        
    }

    def postlist = {
        def posts = Post.list()

        [postInstanceList: posts,totalPosts:posts.size()]
    }

    def schoollist = {
        if (!params.max) {
			params.max = 10
		}
        def schools = School.list(params)

        [schoolInstanceList: schools,totalSchools:School.count()]
    }
 

    def postDelete = {
        def postInstance = Post.get(params.id)
        if (postInstance) {
            flash.message = "删除了 ${postInstance.author.name}的'${postInstance.title}' "
            postInstance.delete()
            
            redirect(action: postlist)
        }else {
            flash.message = "没有找到您要删除的博文"
            redirect(action: postlist)
        }
    }

    def searchPost = {
        def searchKey = params.searchKey
        def postInstanceList = new ArrayList()
        if(searchKey){
            postInstanceList = Post.withCriteria() {
                like('title', '%'+searchKey+'%')
            }
        }
        render(view:'postlist',model:[postInstanceList: postInstanceList,totalPosts:postInstanceList.size()])
    }

    def goswitchSchool = {
        if (params.id) {
            def school = School.get(params.id)
            school.enable = !school.enable
            school.save(flush: true)
        }
        redirect(action: "schoollist")
    }

    def searchSchool = {
        def searchKey = params.searchKey
        def schoolInstanceList = new ArrayList()
        if(searchKey){
            schoolInstanceList = School.withCriteria() {
                like('name', '%'+searchKey+'%')
            }
        }
        render(view:'schoollist',model:[schoolInstanceList: schoolInstanceList,totalSchools:schoolInstanceList.size()])
    }
}
